package hotel.quartz.oversea.job.meituan;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.quartz.UrlMtEnums;
import hotel.base.oversea.utils.Result;
import hotel.quartz.oversea.job.base.BaseJob;
import hotel.quartz.oversea.job.task.MeituanHotelOrderQueryTask;
import hotel.quartz.oversea.vo.meituan.MtHotelOrderBookingDealingDto;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Meituan Hotel Query Job
 * </p>
 *
 * @description: Meituan Hotel Query Job
 * @author: qiuyy
 * @date: 2020-05-26 13:22
 * @version: V1.0
 */
@Slf4j
public class MeituanHotelOrderQueryJob implements BaseJob {
    private String timestamp = "Mhoqj"+String.valueOf(System.currentTimeMillis());
    @Autowired
    private MeituanHotelOrderQueryTask meituanHotelOrderQueryTask;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 定时器频率：每一分钟查询一次
     * 业务：主动查询处理中订单，且这些订单未过去一天到现在的订单；
     * 描述：主动查询订单状态为“已提交待确认”“取消中”且时间为过去一天到现在的订单  （配合上游的主动推送相结合保证订单状态变更的及时获知）；
     *       订单来源：美团酒店订单表 mt_hotel_order_booking
     * 涉及接口：1、查询符合条件的订单集合
     *           2、查询上游接口 queryOrder
     *
     * @param jobExecutionContext
     */
    @Override
    @SneakyThrows
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("Meituan Hotel Query Job {} >>>>>>>> 酒店主动查询订单定时器执行时间: {}",timestamp, DateUtil.now());

        //正式：ip+端口+网关+mapping地址?access_token//本地联调：ip+端口+mapping地址?access_token
        String host = CommonUrlConstants.MEITUAN_HOST;

        //美团接口，查询出订单表处理中的订单
        String dealingOrderUrl = host+ UrlMtEnums.hotelOrderQuery.QUERY_DEALING_ORDER.getValue();//+"?access_token="+accessToken
        //美团接口，查询上游订单状态
        String orderQueryUrl = host+ UrlMtEnums.hotelOrderQuery.ORDER_QUERY.getValue();//+"?access_token="+accessToken;

        //获取符合的订单集合
        String today = hotel.base.oversea.utils.DateUtil.formatDate(new Date(),"yyyy-MM-dd");
        String beforeDay = hotel.base.oversea.utils.DateUtil.getFrontDay(today,-1);//过去一天内处理中的订单，取消中或确定中
        List<MtHotelOrderBookingDealingDto> dealingOrderIds =  requestPageDealingOrder(dealingOrderUrl,beforeDay);
        log.info("Meituan Hotel Query Job {} 酒店主动查询订单,存在需查询的处理中订单 dealingOrderIds: {},{}",dealingOrderIds,timestamp, DateUtil.now());
        if(null != dealingOrderIds && dealingOrderIds.size()>0){
            //查询订单，上送多个，接口里出来订单状态修改保存日志等操作
            meituanHotelOrderQueryTask.executeOrderQuery(timestamp,dealingOrderIds,orderQueryUrl);
        }
        log.info("Meituan Hotel Query Job {} >>>>>>>> 酒店主动查询订单定时器结束时间: {},{}",timestamp, DateUtil.now());
    }
    /**
     * 获取订单，过去一天内状态为待确认的订单
     */
    private List<MtHotelOrderBookingDealingDto> requestPageDealingOrder(String pageDealingOrderUrl, String gtTime) {

        Map<String,String> map = new HashMap<>();
        map.put("gtTime",gtTime);
        //发起请求
        log.info("Meituan Hotel Query Job {} 酒店主动查询订单定时器进入方法 分页查询处理中酒店 requestPageDealingOrder map={}",timestamp, JSON.toJSONString(map));
        Result response = JobRestTemplateUtils.post(pageDealingOrderUrl, JSON.toJSONString(map),restTemplate);
        List<MtHotelOrderBookingDealingDto> dtoIds = null;
        if(response.getCode()== CommonConstants.SUCCESS){
            if(null != response.getData()){
                dtoIds = JSON.parseArray(response.getData().toString(), MtHotelOrderBookingDealingDto.class);
            }
        }else{
            log.info("Meituan Hotel Query Job {} 酒店主动查询订单定时器进入方法,过去一天内状态为待确认的订单, 获取失败：{}",timestamp, JSON.toJSONString(response));
        }
        return dtoIds;
    }


}
